<?php

namespace App\Models\CacheModel\zl4;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class Zl4GoodTimeTag extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl4_good_time_tag';

    protected $cacheModel = 'horoscope.good_time_tag';

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {   // 获取未删除 的数据
                $data = $this
                    ->where(['id' => ['in', $ids]])
                    ->column('id,tid,gid,sort,name,extend,type,create_time', 'id');
            } else
            {
                $data = $this->where(['tid' => $type[0], 'status' => 1])->order('sort desc')->column('id,tid,gid,type,sort,name,extend,create_time', 'id');
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'tid', 'gid', 'type', 'sort', 'create_time']);

            if (isset($itemData['extend']))
            {
                $data[$index]['extend'] = $itemData['extend'] ? json_decode($itemData['extend'], true) : [];
            }
        }

        return $data;
    }

    /**
     * 获取类型所有标签
     * @param int $type
     * @return mixed
     * @throws \Exception
     */
    public function getTypeAllTids(int $type)
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $type)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($type)
            {
                try
                {
                    $where = ['tid' => $type, 'status' => 1];
                    $data = $this->where($where)->column('sort', 'id');
                    $resData = [];
                    foreach ($data as $id => $sort)
                    {
                        $resData[$id] = ['id' => $id, 'sort' => $sort + ($id / 10000)];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'sort', 0);

        return $resData['data'];
    }

    /**
     * 获取 吉日吉时 首页 事项标签信息
     * @param array $tags
     * @return array
     * @throws \Exception
     */
    public function getTypesAllTids(array $tags)
    {
        $resData = [];
        $allTids = [];
        foreach ($tags as $tagData)
        {
            $tids = $this->getTypeAllTids($tagData['id']);

            $tids = array_keys($tids);

            $resData['tabs'][] = [
                'name' => $tagData['name'],
                'icon' => $tagData['icon'],
                'tids' => $tids
            ];

            $allTids = array_merge($allTids, $tids);
        }

        $tags = $this->batchGetInfoByIds($allTids, ['id', 'name', 'type']);

        $resData['tags'] = array_values($tags);

        return $resData;
    }
}
